package com.mediacloud.app.newsmodule.fragment.topic;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.mediacloud.app.assembly.banner.NetImageBanner;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.BaseMessageReciver;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.TopicDetailDataReciver;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.component.BannerView;
import com.mediacloud.app.newsmodule.addnewslike.m.AddLikeDataInvoke;
import com.mediacloud.app.newsmodule.addnewslike.p.NewsLikePresenter;
import com.mediacloud.app.newsmodule.addnewslike.v.ILikesNumUpdate;
import com.mediacloud.app.newsmodule.fragment.newslist.NewsListWidthBannerFragment;
import com.mediacloud.app.newsmodule.utils.TopicListDataInvoker;
import com.mediacloud.app.newsmodule.utils.share.ShareWrap;
import com.mediacloud.app.newsmodule.view.ResizeLoadImageBanner;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.mediacloud.app.user.model.UserInfo;

public class TopicItemDetailFragment extends NewsListWidthBannerFragment implements ILikesNumUpdate
{
	protected TextView mTopicLeadTxt;
	//protected SharePopGridWindow sharePopWindow;
	protected ArticleItem articleItem;
	//protected ShareGridDataUtil shareGridDataUtil;
	protected ShareWrap shareWrap;
	NewsLikePresenter newsLikePresenter;
	public TopicItemDetailFragment()
	{
	}


	@Override
	protected void initOther()
	{
		super.initOther();
		newsLikePresenter=new NewsLikePresenter(getActivity(),this);
		articleItem= saveInstanceArguments.getParcelable("topic");
//		sharePopWindow=new SharePopGridWindow(getActivity());
//		sharePopWindow.setShareGirdListener(new ShareGirdItemListener());
//		shareGridDataUtil=new SBShareUtils();
//		shareGridDataUtil.initBaseShareGridData(getActivity());
		shareWrap =new  ShareWrap(requireContext(),false);
		shareWrap.setShowFunction(true);
		shareWrap.init(articleItem,new CatalogItem());
	}
	@Override
	protected void addBanner()
	{
		View topicLeadView=LayoutInflater.from(getActivity()).inflate(R.layout.topicheader_leadtxt, null);
		mTopicLeadTxt=Utility.findViewById(topicLeadView, R.id.mTopicLeadTxt);
//		banner=new ResizeLoadImageBanner(getActivity());
//		int mainColor=Color.parseColor(AppFactoryGlobalConfig.getAppServerConfigInfo(getActivity()).getColor());
//		banner.currentFocusItemColor=mainColor;
//		banner.defaultLoadBackground=AppFactoryGlobalConfig.getDefaultImagLoadBackground();
//		banner.DefaultDelta=(float) (1.0/2.0f);
//		int bannerHeight=(int) (getResources().getDisplayMetrics().widthPixels*banner.DefaultDelta);
//		banner.defaultHeight=bannerHeight;
//		headerViewContainer.addView(banner);
//		banner.bottomLabelLayout.setVisibility(View.GONE);
//		headerViewContainer.addView(topicLeadView);

		bannerView = new BannerView(requireContext());
		ViewGroup.LayoutParams  lp = new ViewGroup.LayoutParams(-1, (int) (Utility.getScreenWidth(requireContext())*1f/2));
		headerViewContainer.addView(bannerView, 0,lp);
		haveBanner = true;
	}
	protected Class<? extends BaseDataInvokeCallBack<? extends BaseMessageReciver>> getDataInvokeCallBackClass()
	{
		return TopicDataInvokeCallBack.class;
	}
	@Override
	protected void initDataInvoker()
	{
		dataInvoker=new TopicListDataInvoker(dataCallback);
	}
	@Override
	protected void getNewsListData()
	{
		if(articleItem.getCid()>0)
		{
			((TopicListDataInvoker)dataInvoker).getTopicDetailById(String.valueOf(articleItem.getCid()), pageNum,pageSize);
		}
		else
		{
			((TopicListDataInvoker)dataInvoker).getArticleById(String.valueOf(articleItem.getId()),UserInfo.getUserInfo(getActivity()).getUserid(),articleItem.getParentId(), pageNum,pageSize);
		}
	}

	public void showSharePanel(View rootView)
	{
		if(shareWrap!=null)
		{
			UserInfo userInfo=UserInfo.getUserInfo(getActivity());
			if(!userInfo.isLogin())
				userInfo.setUserid(UserInfo.UserInfo);
			//sharePopWindow.setShareGridAdaptorData(shareGridDataUtil.BaseShareGridData);
			shareWrap.show(rootView);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	public boolean onBackPressed() {
		if(shareWrap!=null&&shareWrap.isShowing())
		{
			shareWrap.dismiss();
			return false;
		}
		return true;
	}
	@Override
	protected void refreshBanner()
	{
		if(bannerData.size()==0||attached==false)
			return;
		bannerView.setData(bannerData);
//		banner.cleanAll();
//		for(ArticleItem item:bannerData)
//		{
//			banner.addItem(item.getLogo(), item.getTitle(),AppFactoryGlobalConfig.getDefaultImageLoadDrawable());
//		}
//		banner.updateLayout();
	}

	@Override
	public void onClick(NetImageBanner banner, int position, Object tag)
	{
	}

//	class ShareGirdItemListener implements OnItemClickListener
//	{
//		@Override
//		public void onItemClick(AdapterView<?> parent, View view, int position,
//								long id)
//		{
//			ShareGridClickUtil.ShareGridClickHandle(shareGridDataUtil, position, articleItem,catalogItem, getActivity());
//		}
//
//	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
							long id) {
		super.onItemClick(parent, view, position, id);
	}
	public class TopicDataInvokeCallBack extends BaseDataInvokeCallBack<TopicDetailDataReciver>
	{
		@Override
		public void success(TopicDetailDataReciver result)
		{
			if(adaptor==null)
				return;
			isLoading=false;
//			mCatalogContentItemListView.setPullLoadEnable(result.more);
			if(pageNum==1)
			{
				mXSmartRefreshLayout.finishRefresh();

				bannerData.clear();
				ArticleItem articleItem=new ArticleItem();
				articleItem.setLogo(result.logo);
				//					articleItem.setLogo("http://stats.chinaz.com/tool_img/juhe_468.png");
				articleItem.setTitle("");
				bannerData.add(articleItem);
				if(TextUtils.isEmpty(result.prop1)==false)
				{
					mTopicLeadTxt.setText(result.prop1);
				}
				else
				{
					mTopicLeadTxt.setText(catalogItem.getCatname());
				}
				//					mTopicLeadTxt.setText("http://stats.chinaz.com/tool_img/juhe_468.png");
				refreshBanner();
				if(adaptor.getListContentData() != null)
					adaptor.getListContentData().clear();
			}
			if(result.more)
				mXSmartRefreshLayout.finishLoadMore();
			else
				mXSmartRefreshLayout.finishLoadMoreWithNoMoreData();
			if (adaptor.getListContentData() == null)
				adaptor.setListContentData(result.articleList);
			else
				adaptor.getListContentData().addAll(
						result.articleList);
			adaptor.notifyDataSetChanged();
		/*	mCatalogContentItemListView.stopLoadMore();
			mCatalogContentItemListView.stopRefresh();*/
			if (!result.state)
			{
				showStateView(TYPE_NO_CONTENT, false);
				//banner.setVisibility(View.INVISIBLE);
				bannerView.setVisibility(View.INVISIBLE);

			}
			else
			{
				closeStateView();
				//banner.setVisibility(View.VISIBLE);
				bannerView.setVisibility(View.VISIBLE);
			}

		}
	}

	@Override
	public void updateLikesNum(int value) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateLikesSuccess(AddLikeDataInvoke.AddLikeDataReciver data) {

	}

	@Override
	public void updateLikesFault(Object data) {

	}

	@Override
	protected void addLBSChosoeHeader()
	{
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if(newsLikePresenter!=null)
		{
			newsLikePresenter.addLikeDataInvoke.destory();
		}
	}
}